/*
 * @version: 1.0
 * @Author: zhl
 * @Date: 2021-12-08 10:40:34
 * @LastEditors: zhl
 * @LastEditTime: 2021-12-15 15:23:14
 * @Description: 串口代理模块封装
 */
#ifndef __SERIAL_PROXY_H__
#define __SERIAL_PROXY_H__

#include <string>
#include <iostream>
#include <memory>

#include "IChannel.h"
#include "SerialEI.h"
#include "CThreadNotify.h"
#include "CMosquittoManager.h"
#include "CConfigManager.h"
#include "SerialPacketManager.h"



class SerialProxy : public IChannel{
private:
    int      c_req_id;
    bool     c_is_receiving;
    int      c_rep_len;
    uint8_t  *c_rep;
    std::string    c_if_port;
    
    SERIAL_PARAMS  c_serial_params;
    SerialPacketManager c_spm;
    std::shared_ptr<CMosquittoManager> c_mosquitto_manager;
   

private:
    void wait_recv_finished(uint32_t timeout);
    void init();
    void init_mqtt();
    void get_if_port();
    int  subscribe_topic();
    void finish_signal();

public:
    std::string    c_if_addr;
    std::shared_ptr<CConfigManager> c_global_conf;
    CThreadNotify  c_thread_notify;

public:

    SerialProxy(std::shared_ptr<CConfigManager> global_conf, std::string&& if_addr) noexcept;
    ~SerialProxy();

    void set_serial_params(SERIAL_PARAMS&& params) noexcept;
    int  send_msg(uint8_t *req, int req_len, uint8_t *rep, int &rep_len, uint32_t timeout=5000);
    int  recv_msg_callback(CALLBACK_TYPE cb_type, void *data, int data_len, int result, void *arg);
    int  connect(const char *host, int port, const char * username, const char *password, int keepalive = 5000);

};








#endif/* __SERIAL_PROXY_H__ */